<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="d" uri="http://dgg.net/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title></title>
    <c:import url="../../header.jsp"/>
    <link rel="stylesheet" href="${baseStaticUrl}/dggui/dggui-3.0/pages/financingSC/productionDetail/productionDetail.css">
    <link rel="stylesheet" href="${baseStaticUrl}/dggui/dggui-3.0/dggplugs/hickiboss2/hickiboss2.css">
    <link rel="stylesheet" type="text/css" href="${basePath}/static/css/production.css?v=${version}"/>


</head>
<body>
<div class="content-warp">
    <div class="dgg-list-top-warp">
    <div class="screen-search">
        <ul>
            <li>
                <label>团队名称：</label>
                <input type="text" class="dggui-input dggui-input-normal" name="keyWords" id="keyWords"
                       placeholder="输入团队名称">
            </li>
            <li>
                <button class="dggui-btn searchBtn" onclick="searchPunish();">搜索</button>
                <button class="dggui-btn dggui-btn-primary" onclick="cleanSearch()">清空条件</button>
            </li>
        </ul>

    </div>
    </div>
    <div class="bg-warp">
        <!--相关生产订单-->
        <c:if test="${type == 0}">
        <h3>
            <button class="dggui-btn searchBtn" onclick="addPunishInfor();">新增罚款</button>
            <c:if test="${type == 0}">
            <button class="dggui-btn searchBtn" onclick="editAll();" id="editAll">编辑</button>
            <button class="dggui-btn searchBtn" style="cursor:pointer;display: none;" onclick="cancelEditAll();" id="cancelEditAll">取消</button>
            </c:if>
        </h3>
        </c:if>
        <table class="dggui-table cell-border table-form" id="editPunishInfor">
            <input type="hidden" id="punishId" value="${punishId}"/>
            <thead>
            <th>团队名称</th>
            <th>扣罚操行分</th>
            <th>扣罚金额</th>
            <c:if test="${type == 0}">
            <th>
                操作
               <%-- <a class="dggui-link" style="cursor:pointer" onclick="editAll();" id="editAll">编辑</a>--%>
               <%-- <a class="dggui-link" style="cursor:pointer;display: none;" onclick="cancelEditAll();" id="cancelEditAll">取消</a>--%>
            </th>
            </c:if>
            </thead>
            <tbody id="editPunishAppend">
            <c:if test="${!empty basicsList}">
                <c:forEach var="other" items="${basicsList}">
                    <tr>
                        <input type="hidden" name="punishinofrId" value="${other.id}">
                        <input type="hidden" name="version" value="${other.version}">
                        <td>${other.teamName}</td>
                        <td>
                            <span name="pointso">${other.conductPoints}</span>
                            <span name="pointst" style="display: none;"><input type="text" value="${other.conductPoints}" class="dggui-input" style="min-width: 60px;width: 60px;min-height: 25px;height: 25px;"></span>
                        </td>
                        <td>
                            <span name="moneyo">${other.money}</span>
                            <span name="moneyt" style="display: none;"><input type="text" value="${other.money}" class="dggui-input" style="min-width: 60px;width: 60px;min-height: 25px;height: 25px;"></span>
                        </td>
                        <c:if test="${type == 0}">
                        <td><c:if test="${other.isBasics == 1}"><a class="dggui-link" style="cursor:pointer" onclick="delPunish('${other.id}','${other.version}');">删除</a></c:if></td>
                        </c:if>
                    </tr>
                </c:forEach>
            </c:if>
            <c:if test="${empty basicsList}">
                <tr>
                    <td colspan="4" style="text-align: center">没有数据</td>
                </tr>
            </c:if>

            </tbody>
        </table>
        <h3 style="display: none;" id="allSava"><button class="dggui-btn searchBtn" style="left: 50%;" onclick="allSava();">全部保存</button></h3>
    </div>
</div>
<c:import url="/WEB-INF/jsp/footer.jsp"/>
<script>

    function addPunishInfor(){
        var punishId = $("#punishId").val();
        dggui.dialog.loading();
        $.ajax({
            url: sysInfo.basePath+"/rzsc/punish/add_punish_infor_tabpage.html",
            dataType:'html',
            data:{punishId:punishId},
            success:function (res){
                dggui.dialog.loading('close');
                dggui.dialog.open({
                    title: '新增扣罚',
                    content: res,
                    area: ["480px","350px"],
                    btn: ["确定", "取消"],
                    yes: function (index) {
                        punishInforCreate(index);
                    }, btn2: function () {
                    }
                })
            }
        });
    }
    function searchPunish(){
        cancelEditAll();
        var teamName = $("#keyWords").val().trim();
        var punishId = $("#punishId").val();
        dggui.dialog.loading();
        $.ajax({
            url: sysInfo.basePath+"/rzsc/punish/search_all_punish_detail.do",
            dataType:'json',
            data:{teamName:teamName,id:punishId},
            success:function (res){
                dggui.dialog.loading('close');
                if(res.code == 0){
                    var data = res.data;
                    var htmlStr = '';
                    if(data.length>0){
                        $(data).each(function(j){
                            var delStr = '';
                            if(data[j].isBasics == 1){
                                delStr = '<a class="dggui-link" style="cursor:pointer" onclick="delPunish(\''+data[j].id+'\',\''+data[j].version+'\')">删除</a>';
                            }
                            var type = "${type}";
                            var delallStr = "";
                            if(type == 0){
                                delallStr = '<td>'+delStr+'</td>';
                            }
                            htmlStr +=  '<tr>'+
                                '<input type="hidden" name="punishinofrId" value="'+data[j].id+'">'+
                                '<input type="hidden" name="version" value="'+data[j].version+'">'+
                                '<td>'+data[j].teamName+'</td>'+
                                '<td>' +
                                '<span name="pointso">'+formartStr(data[j].conductPoints)+'</span>' +
                                '<span name="pointst" style="display: none;"><input type="text" value="'+formartStr(data[j].conductPoints)+'" class="dggui-input" style="min-width: 60px;width: 60px;min-height: 25px;height: 25px;"></span>' +
                                '</td>'+
                                '<td>' +
                                '<span name="moneyo">'+formartStr(data[j].money)+'</span>' +
                                '<span name="moneyt" style="display: none;"><input type="text" value="'+formartStr(data[j].money)+'" class="dggui-input" style="min-width: 60px;width: 60px;min-height: 25px;height: 25px;"></span>' +
                                '</td>'+delallStr+
                                '</tr>';
                        });
                    }else{
                        htmlStr =   '<tr>' +
                                    '<td colspan="4" style="text-align: center">没有数据</td>' +
                                    '</tr>'
                    }

                    $("#editPunishAppend").html(htmlStr);
                }else{
                    dggui.dialog.errors(res.msg);
                }
            },
            error: function () {
                dggui.dialog.loading('close');
                dggui.dialog.errors('保存失败，请联系管理员！！');
            }
        });
    }
    function delPunish(id,version){
        var punishId = $("#punishId").val();
        dggui.dialog.confire("确定删除吗？", '删除提示', function () {
            dggui.dialog.loading();
            $.ajax({
                url: sysInfo.basePath+"/rzsc/punish/edit_punish_infor.do",
                dataType:"json",
                type:'post',
                data:{id:id,flag:0,version:version,punishId:punishId},
                success:function(data){
                    dggui.dialog.loading('close');
                    console.log(data);
                    if (data.code == 0) {
                        dggui.dialog.succeed('操作成功！')
                        //刷新列表
                        searchPunish();
                    } else {
                        dggui.dialog.errors(data.msg);
                    }
                },
                error:function(){
                    dggui.dialog.loading('close');
                    dggui.dialog.errors('保存失败，请联系管理员！！');
                }
            });
        });
    }
    function editAll(){
        $("#allSava").show();
        $("#cancelEditAll").show();
        $("#editAll").hide();
        $("[name=pointso]").hide();
        $("[name=pointst]").show();
        $("[name=moneyo]").hide();
        $("[name=moneyt]").show();
    }
    function cancelEditAll(){
        $("#allSava").hide();
        $("#cancelEditAll").hide();
        $("#editAll").show();
        $("[name=pointso]").show();
        $("[name=pointst]").hide();
        $("[name=moneyo]").show();
        $("[name=moneyt]").hide();
    }
    function formartStr(str){
        if(String(str).indexOf(".") == -1){
            return String(str)+".00";
        }else{
            return str;
        }

    }
    function ObjStory(id,conductPoints,money,version){
        this.id = id;
        this.conductPoints = conductPoints;
        this.money = money;
        this.version = version;
    }
    function allSava(){
        var punishLitst = $("#editPunishAppend").children("tr");
        var tableparms = new Array();
        var b = true;
        $(punishLitst).each(function(j){
            var id = $(this).find("[name=punishinofrId]").val();//扣罚ID
            var conductPoints = $(this).find("[name=pointst]").find("input").val().trim();//操行分
            var money = $(this).find("[name=moneyt]").find("input").val().trim();//罚款
            var version = $(this).find("[name=version]").val();//版本号version
            var reg=/^(((\d|[1-9]\d)(\.\d{1,2})?)|100|100.0|100.00)$/;
            var regPoints = /^(0(\.\d{1,2})?|1(\.0{1,2})?)$/;

            if(conductPoints == ''){
                dggui.dialog.tip('数值不能为空！',$(this).find('[name=pointst]'));
                b = false;
                return false;
            }
            if(!regPoints.test(conductPoints)){
                dggui.dialog.tip('操行分0-1之间，可保留小数点后两位！',$(this).find('[name=pointst]'));
                b = false;
                return false;
            }

            if(money == ''){
                dggui.dialog.tip('数值不能为空！',$(this).find('[name=moneyt]'));
                b = false;
                return false;
            }
            if(!reg.test(money)){
                dggui.dialog.tip('金额0-100之间，可保留小数点后两位！',$(this).find('[name=moneyt]'));
                b = false;
                return false;
            }

            var objParam = new ObjStory(id, conductPoints, money, version);
            tableparms[j] = objParam;
        });

        if(!b){
            return;
        }
        var json = JSON.stringify(tableparms);
        /*var params = $('#punishEdit').serializeArray();
        params.push({name: 'jsonData', value: json});*/
        var punishId = $("#punishId").val();
        dggui.dialog.loading();
        $.ajax({
            url: sysInfo.basePath+"/rzsc/punish/edit_punish_detail_list.do",
            dataType:"json",
            type:'post',
            data:{params:json,punishId:punishId},
            success:function(data){
                dggui.dialog.loading('close');
                console.log(data);
                if (data.code == 0) {
                    dggui.dialog.succeed('操作成功！')
                    //刷新列表
                    searchPunish();
                } else {
                    dggui.dialog.errors(data.msg);
                }
            },
            error:function(){
                dggui.dialog.loading('close');
                dggui.dialog.errors('保存失败，请联系管理员！！');
            }
        });

    }
</script>
</body>
</html>